home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / LDB171.ARJ / EXAMP601.CPP < prev    next >
C/C++ Source or Header  |  1992-05-12  |  2KB  |  109 lines

  1.     // examp601.cpp - link with binder.obj, mutual.obj,
  2.     // mint.obj, and mstr.obj
  3.     
  4.     #define bfile  "examp601.txt"
  5.  
  6.     #include <string.h>
  7.     #include "mint.hpp"
  8.     #include "mstr.hpp"
  9.  
  10.     // display integers and strings
  11.  
  12.     void display(MutuaL M)
  13.     {
  14.         switch (M->ID())  {
  15.         case ID_Mint:
  16.             cout << ((MinT)M)->I() << endl;
  17.             break;
  18.         case ID_Mstr:
  19.             cout << ((MstR)M)->S() << endl;
  20.             break;
  21.         default:
  22.             cout << "unknown" << endl;
  23.             break;
  24.         }
  25.     }
  26.  
  27.  
  28.     // sort integers and strings
  29.  
  30.     int mintstrcmp(MutuaL M1, MutuaL M2)
  31.     {
  32.         // integers sorted to the front
  33.         // strings sorted to the rear
  34.  
  35.         if (M1->ID() == ID_Mint)
  36.             if (M2->ID() == ID_Mint)
  37.                 return ((MinT)M1)->I()
  38.                 - ((MinT)M2)->I();
  39.             else
  40.                 return -1;
  41.         else
  42.             if (M2->ID() == ID_Mint)
  43.                 return 1;
  44.             else
  45.                 return strcmp(
  46.                     ((MstR)M1)->S(),
  47.                     ((MstR)M2)->S());
  48.     }
  49.  
  50.  
  51.     main()
  52.     {
  53.         Mint::RegisterClass();
  54.         Mstr::RegisterClass();
  55.  
  56.  
  57.         MBinder b1;
  58.  
  59.         b1.push(new Mstr("Hello LDB!"));
  60.         b1.insQ(new Mstr("Goodbye linked"));
  61.         b1.insQ(new Mstr("list programming!"));
  62.         b1.insQ(b1.rear());
  63.         b1.insQ(new Mstr(
  64.             "Line above tests multilinking!"));
  65.  
  66.         for (int i = 3; i; i--)
  67.             b1.insQ(new Mint(i));
  68.  
  69.         cout << "\n\nMBinder of streamable integers"
  70.             << " and strings!\n\n";
  71.  
  72.         b1.forEach((BDRapplY)display);
  73.  
  74.         cout << "\n\nPress enter to continue ...";
  75.         cin.get();
  76.  
  77.  
  78.         b1.setComP((BDRcomP)mintstrcmp);
  79.  
  80.         Binder::RegisterComP((BDRcomP)mintstrcmp);
  81.  
  82.         (void) b1.save(bfile);
  83.  
  84.         b1.allDel();
  85.  
  86.  
  87.         MBinder b2(bfile);
  88.  
  89.         cout << "\nStreamed and "
  90.             << "reloaded MBinder with "
  91.             << "multiple links maintained "
  92.             << "\nand sorted with streamed "
  93.             << "compare fnc, ints in front:"
  94.             << " \n";
  95.  
  96.  
  97.         Restream();
  98.  
  99.             // Don't load again from
  100.             // any stream without
  101.             // restreaming StreamRegistry!!!
  102.  
  103.         b2.sort();
  104.  
  105.         b2.forEach((BDRapplY)display);
  106.  
  107.         return 0;
  108.     }
  109.